<?php
/**
 * Created by PhpStorm.
 * User: lh
 * Date: 2020/1/20
 * Time: 11:55
 */

namespace App\Services\Com;


use App\Models\Activity\Activity;
use App\Models\Activity\Join;
use App\Models\Chat\ChatMessage;
use App\Models\Common\HandleLog;
use App\Models\Common\Log;
use App\Models\Group\ChatGroupMessage;
use App\Models\Group\Group;
use App\Models\Member\Collect;
use App\Models\Member\Comment;
use App\Models\Member\Follow;
use App\Models\Member\Friend;
use App\Models\Member\Like;
use App\Models\Member\Member;
use App\Models\Member\Read;
use App\Models\Member\Report;
use App\Models\Member\Share;
use App\Models\Posts\Posts;
use App\Models\Posts\PostsByUser;
use App\Models\Store\CheckIn;
use App\Models\Store\Store;
use App\Models\Vote\Vote;
use App\Models\Work\Question;
use Illuminate\Support\Facades\Cache;

class StatisticsService
{
    /**
     * 后台统计数据汇总
     * @param $param
     * @return array
     * @throws \Exception
     */
    public static function adminIndex($param): array
    {
        try {
            $result = [];
            //API调用总数
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'API调用总数', 'value'=>Log::where($where)->count('*')];

            //用户数量
            $where = [];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'用户数量', 'value'=>Member::where($where)->count('*')];

            //活跃用户
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'活跃用户', 'value'=>Log::where($where)->groupBy('member_uuid')->get()->count()];

            //登录总数
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[''] = ['key'=>'登录总数', 'value'=>Log::where($where)->whereIn('url', [
                'api/client/auth/login_mobile_captcha',
                'api/client/auth/sms_captcha',
                'api/client/auth/login_wechat',
                'api/client/auth/login_apple',
                ])->count('*')];

            //聊天数量
            $where = [];
            !empty($param['start_time']) && $where[] = ['chat_day', '>=', date('Ymd', strtotime($param['start_time']))];
            !empty($param['end_time']) && $where[] = ['chat_day', '<=', date('Ymd', strtotime($param['end_time']))];
            $result[] = ['key'=>'聊天数量', 'value'=>\App\Models\Chat\ChatGroupMessage::where($where)->count('*') + ChatMessage::where($where)->count('*')];

            //新增用户数量
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增用户数量', 'value'=>Member::where($where)->count('*')];

            //新增talking数量
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增talking数量', 'value'=>Group::where($where)->count('*')];

            //审核通过数量
            $where = [];
            $where[] = ['type', '=', 'member_state_success'];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'审核通过数量', 'value'=>HandleLog::where($where)->count('*')];

            //待审核数量（需标红）
            $where = [];
            $where[] = ['state', '=', 5];
            !empty($param['start_time']) && $where[] = ['education_submit_time', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['education_submit_time', '<=', $param['end_time']];
            $result[] = ['key'=>'待审核数量', 'value'=>Member::where($where)->count('*')];

            //审核拒绝数量
            $where = [];
            $where[] = ['type', '=', 'member_state_fail'];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'审核拒绝数量', 'value'=>HandleLog::where($where)->count('*')];

            //新增信息流数量
            $where = [];
            $where[] = ['state', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增信息流数量', 'value'=>Posts::where($where)->count('*')];

            //新增投票数量
            $where = [];
            $where[] = ['state', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增投票数量', 'value'=>Vote::where($where)->count('*')];

            //新增活动数量
            $where = [];
            $where[] = ['admin_state', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增活动数量', 'value'=>Activity::where($where)->count('*')];

            //新增店铺数量
            $where = [];
            $where[] = ['state', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增店铺数量', 'value'=>Store::where($where)->count('*')];

            //新增信息流评论数量
            $where = [];
            $where[] = ['place', '=', 5];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增信息流评论数量', 'value'=>Comment::where($where)->count('*')];

            //新增新闻评论数量
            $where = [];
            $where[] = ['place', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增新闻评论数量', 'value'=>Comment::where($where)->count('*')];

            //新增投票评论数量
            $where = [];
            $where[] = ['place', '=', 2];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增投票评论数量', 'value'=>Comment::where($where)->count('*')];

            //新增新闻点赞数量
            $where = [];
            $where[] = ['place', '=', 1];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增新闻点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增活动点赞数量
            $where = [];
            $where[] = ['place', '=', 3];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增活动点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增店铺点赞数量
            $where = [];
            $where[] = ['place', '=', 4];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增店铺点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增信息流点赞数量
            $where = [];
            $where[] = ['place', '=', 5];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增信息流点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增用户点赞数量
            $where = [];
            $where[] = ['place', '=', 6];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增用户点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增talking点赞数量
            $where = [];
            $where[] = ['place', '=', 7];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增talking点赞数量', 'value'=>Like::where($where)->count('*')];

            //新增举报数量（需标红）
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增举报数量', 'value'=>Report::where($where)->count('*')];

            //新增问题工单数量（需标红）
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增问题工单数量', 'value'=>Question::where($where)->count('*')];

            //新增店铺打卡数量
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增店铺打卡数量', 'value'=>CheckIn::where($where)->count('*')];

            //新增活动参与者数量
            $where = [];
            !empty($param['start_time']) && $where[] = ['created_at', '>=', $param['start_time']];
            !empty($param['end_time']) && $where[] = ['created_at', '<=', $param['end_time']];
            $result[] = ['key'=>'新增活动参与者数量', 'value'=>Join::where($where)->whereIn('state', [-5,1,10])->count('*')];
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }

        return $result;
    }
    //查询业务统计数据
    public static function get(string $service_uuid, string $parent_uuid = ''){
        // $cache_key = config('cache.key_list.statistics_service_uuid').md5($service_uuid);
        // $cache_data = Cache::get($cache_key);
        // if(!empty($cache_data)){
        //     return $cache_data;
        // }
        $statistics = [];
        $statistics['like_num'] = Like::where('service_uuid', $service_uuid)->count('*');
        $statistics['collect_num'] = Collect::where('service_uuid', $service_uuid)->count('*');
        $statistics['share_num'] = Share::where('service_uuid', $service_uuid)->count('*');
        if($parent_uuid){
            $statistics['comment_num'] = Comment::where('parent_uuid', $parent_uuid)->where('is_delete', 0)->count('*');
        }else{
            $statistics['comment_num'] = Comment::where('service_uuid', $service_uuid)->where('is_delete', 0)->count('*');
        }
        $statistics['read_num'] = Read::where('service_uuid', $service_uuid)->count('*');;
        // Cache::put($cache_key, $statistics, config('cache.ttl_stat'));

        return $statistics;
    }

    //查询用户统计数据
    public static function getByMember(string $member_uuid){
         $cache_key = config('cache.key_list.statistics_by_member').md5($member_uuid);
         $cache_data = Cache::get($cache_key);
         if(!empty($cache_data)){
             return $cache_data;
         }
        $statistics = [];
        $statistics['like_num'] = Like::where('member_uuid', $member_uuid)->count('*');
        $statistics['collect_num'] = Collect::where('member_uuid', $member_uuid)->count('*');
        $statistics['share_num'] = Share::where('member_uuid', $member_uuid)->count('*');
        $statistics['comment_num'] = Comment::where('member_uuid', $member_uuid)->where('is_delete', 0)->count('*');
        $statistics['follow_num'] = Follow::where('member_uuid', $member_uuid)->count('*');
        $statistics['followed_num'] = Follow::where('followed_member_uuid', $member_uuid)->count('*');
        $statistics['friend_num'] = Friend::where('member_uuid', $member_uuid)->count('*');
        $statistics['posts_num'] = PostsByUser::where('member_uuid', $member_uuid)->count('*');
        $statistics['read_num'] = Read::where('member_uuid', $member_uuid)->count('*');;

         Cache::put($cache_key, $statistics, config('cache.ttl_stat'));

        return $statistics;
    }
}